<template>
  <div>
    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
      <el-tab-pane label="用户注销" name="first">
        <el-row class="disfena">
          <el-col>
            <!-- <p>已注册的用户<span>asea</span></p> -->
          </el-col>
          <el-col style="text-align: right;">
            <el-button type="primary" @click="zhuxiaus">注销</el-button>
          </el-col>
        </el-row>
        <com-table ref="comTable" :tableData="tableData" url="system/log/local" keyName="XPRO_ARRAY" :has-data="hasData"
          :table-head-list="tableHeadList" :total-count="totalCount" :page-num="pageNum" :searchForm="identform"
          :page-size="pageSize" :page-sizes="pageSizes" :table-loading="loading" :table-height="tableHeight"
          :is-show-select="true" :page-show="false" :handle-show="false" :merge-flag="false"
          @selectTableData="selectTableData" @setSearchHeight="setTableHeight">
        </com-table>
      </el-tab-pane>
      <el-tab-pane label="用户注册" name="second">
        <el-form label-width="80px" ref="identform" :model="identform" style="margin-top: 20px;" :rules="rules">
          <el-row>
            <el-col :span="12">
              <el-form-item label="账户" prop="Account">
                <el-input placeholder="请输入账户" v-model="identform.Account" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="真实姓名" prop="Name">
                <el-input placeholder="请输入真实姓名" v-model="identform.Name" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="角色" prop="Role">
                <el-select v-model="identform.Role" clearable placeholder="请选择">
                  <el-option v-for="item in options" :key="item.label" :label="item.label" :value="item.label">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="邮箱">
                <el-input placeholder="请输入邮箱" v-model="identform.Email" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="OrgId">
                <el-input placeholder="请输入部门" v-model="identform.OrgId" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="电话">
                <el-input placeholder="请输入电话" v-model="identform.Telephone" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <p class="somius">说明：<span>*</span>为必填字段，请到安全保密员账户完成绑定操作 </p>
          </el-row>
          <el-row>
            <el-button class="zhush" type="primary" @click="editpins('identform')">注册</el-button>
          </el-row>
        </el-form>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
  
<script>
import ComTable from "../../components/table/comTable";
export default {
  name: "phonementcation",
  data() {
    return {
      accountNameOnline: '',
      options: [
        {
          label: "系统管理员",
        },
        {
          label: "安全管理员",
        },
        {
          label: "审计管理员",
        },
        {
          label: "用户",
        },
      ],
      pageNum: 1, // 当前页
      pageSize: 10,
      pageSizes: [5, 10, 20, 100],
      totalCount: 0, //共多少条数据
      tableHeight: "", // 表格高度
      hasData: true,
      tableData: [],
      identform: {
        Account: "",
        Name: "",
        Role: "",
        OrgId: "",
        Email: "",
        Telephone: "",
      },
      loading: false,
      activeName: "first",
      tableHeadList: [
        {
          colDisabled: false,
          colHidden: false,
          colSearch: true,
          colSort: false,
          columnComment: "账号",
          columnName: "account",
        },
        {
          colDisabled: false,
          colHidden: false,
          colSearch: true,
          colSort: false,
          columnComment: "角色",
          columnName: "role",
        },
        {
          colDisabled: false,
          colHidden: false,
          colSearch: true,
          colSort: false,
          columnComment: "部门",
          columnName: "orgId",
        },
        {
          colDisabled: false,
          colHidden: false,
          colSearch: true,
          colSort: false,
          columnComment: "姓名",
          columnName: "name",
        },
        {
          colDisabled: false,
          colHidden: false,
          colSearch: true,
          colSort: false,
          columnComment: "电话",
          columnName: "telephone",
        },
        {
          colDisabled: false,
          colHidden: false,
          colSearch: true,
          colSort: false,
          columnComment: "邮箱",
          columnName: "email",
        },
      ],
      rules: {
        Account: [{ required: true, message: "请输入账户", trigger: "blur" }],
        Name: [{ required: true, message: "请输入真实姓名", trigger: "blur" }],
        Role: [{ required: true, message: "请输入角色", trigger: "blur" }],
        OrgId: [{ required: true, message: "请输入部门", trigger: "blur" }],
      },
      zhhuaifo: {},
      dhuslis: [],
      userName: window.sessionStorage.getItem("userName"),
      timestamp: "", //时间戳
    };
  },
  created() {
    this.timestamp = new Date().getTime();
    this.setTableHeight();
    // this.emzenr();
    this.getUserData()
  },
  components: {
    ComTable,
  },
  methods: {
    handleClick(tab, event) {
      console.log(tab, event);
      if (tab.label == "用户注销") {
        this.emzenr();
      } else if (tab.label == "用户注册") {
      }
    },
    setTableHeight() {
      let clientHeiht =
        window.innerHeight || document.documentElement.clientHeight;
      this.tableHeight = clientHeiht - 290;
    },
    emzenr() {
      this.$get("/aqdl/bjtx/list_user").then((res) => {
        console.log(res);
        this.tableData = res.data.data.userArray;
      });
    },
    editpins(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(111);
          let data = {
            account: this.identform.Account,
            name: this.identform.Name,
            orgId: this.identform.OrgId,
            telephone: this.identform.Telephone,
            email: this.identform.Email,
            createTime: "xxxx",
            role: this.identform.Role,
            del: "0",
          };
          this.$postForJson("/aqdl/bjtx/set_user", data).then((res) => {
            console.log(res);
            if (res.statusText == "OK") {
              this.$message({
                message: "注册成功!",
                type: "success",
              });
              this.setlogrlist("294", res.statusText, "成功");
            } else {
              this.setlogrlist("294", res.statusText, "失败");
            }
            this.identform = {};
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    selectTableData(val) {
      this.zhhuaifo = val[0];
      this.dhuslis = val;
    },
    // 注销
    zhuxiaus() {
      if (this.dhuslis.length == "0") {
        this.$message({
          message: "请选择一条数据",
          type: "warning",
        });
      } else {
        let data = {
          account: this.zhhuaifo.account,
          name: this.zhhuaifo.name,
          orgId: this.zhhuaifo.orgId,
          telephone: this.zhhuaifo.telephone,
          email: this.zhhuaifo.email,
          createTime: "xxxx",
          role: this.zhhuaifo.role,
          del: "1",
        };
        this.$postForJson("/aqdl/bjtx/set_user", data).then((res) => {
          console.log(res);
          if (res.statusText == "OK") {
            this.$message({
              message: "注销成功!",
              type: "success",
            });
            // this.emzenr();
            this.setlogrlist("295", res.statusText, "成功");
          } else {
            this.setlogrlist("295", res.statusText, "失败");
          }
        });
      }
    },
    //获取账户
    getUserData() {
      this.$get("/aqdl/user/getloginuser").then((res) => {
        console.log(res, 293);
        this.accountNameOnline = res.data.data
        this.emzenr();
      });
    },
    //10位时间戳转换
    getLocalTime(nS) {
      let time = new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ').replace(/\//g, '-');
      // 找出出现空格的位置
      let space_position = time.indexOf(' ');
      // 截取并返回数据
      return time.slice(0, space_position);
    },
    // 记录操作日志接口
    setlogrlist(tyid, sults, redusl) {
      let datess = Date.now()
      datess = (datess + '').substring(0, 10)
      console.log(new Date(this.getLocalTime(datess) + ''))
      let eTimes = new Date(this.getLocalTime(datess) + '').getTime();
      eTimes = parseInt(eTimes) / 1000
      console.log(eTimes, "Timestamp")
      let that = this
      let daJson = {
        Root: {
          LogType: "50",
          LogRecord: {
            "Timestamp": eTimes + '',
            // EmpID: "1",
            // EmpName: "test",
            Type: tyid,
            // KeySerial: "2DD66DF1BEDB7C895C",
            // KeyType: "0",
            Account: this.accountNameOnline,
            Role: 'admin',
            Result: sults + '',
            ResultDesc: redusl,
          },
        },
      };
      let daJsons = {
        Root: {
          LogType: "50",
          LogRecord: {
            "Timestamp": eTimes + '',
            // EmpID: "1",
            // EmpName: "test",
            Type: tyid,
            // KeySerial: "2DD66DF1BEDB7C895C",
            // KeyType: "0",
            Account: this.accountNameOnline,
            Role: '0',
            Result: sults + '',
            ResultDesc: redusl,
          },
        },
      };
      this.$postForJson("/aqdl/auditlog/setlogrecord", daJson).then((res) => {
        console.log(res);
        if (tyid == '295' && redusl == '成功') {
          that.emzenr()
        } else if (tyid == '295' && redusl == '失败') {
          that.emzenr()
        }
      });
      this.$postForJson("/aqdl/auditlog/setlogrecord", daJsons).then((res) => {
        console.log(res);
      });
    },
  },
};
</script>
  
<style scoped>
.disfena {
  display: flex;
  margin-top: 10px;
  margin-bottom: 8px;
}

.somius {
  margin-left: 120px;
  margin-top: 10px;
}

.zhush {
  margin-top: 30px;
  margin-left: 42%;
}
</style>